package com.jzo2o.customer.service.impl;

import com.jzo2o.common.expcetions.DBException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.common.utils.CollUtils;
import com.jzo2o.common.utils.ObjectUtils;
import com.jzo2o.customer.enums.CertificationStatusEnum;
import com.jzo2o.customer.mapper.AgencyCertificationMapper;
import com.jzo2o.customer.model.domain.AgencyCertification;
import com.jzo2o.customer.model.domain.AgencyCertificationAudit;
import com.jzo2o.customer.mapper.AgencyCertificationAuditMapper;
import com.jzo2o.customer.model.dto.request.AgencyCertificationAuditAddReqDTO;
import com.jzo2o.customer.model.dto.request.AgencyCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.model.dto.request.CertificationAuditReqDTO;
import com.jzo2o.customer.model.dto.response.AgencyCertificationAuditResDTO;
import com.jzo2o.customer.service.IAgencyCertificationAuditService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.mvc.utils.UserContext;
import com.jzo2o.mysql.utils.PageHelperUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 * 机构认证审核表 服务实现类
 * </p>
 *
 * @author Echo
 * @since 2025-03-08
 */
@Service
@RequiredArgsConstructor
public class AgencyCertificationAuditServiceImpl extends ServiceImpl<AgencyCertificationAuditMapper, AgencyCertificationAudit> implements IAgencyCertificationAuditService {
    private final AgencyCertificationMapper agencyMapper;

    /**
     * 机构认证审核
     * @param dto
     */
    @Override
    public void saveCertification(AgencyCertificationAuditAddReqDTO dto) {
        AgencyCertificationAudit audit = BeanUtils.copyBean(dto, AgencyCertificationAudit.class);
        audit.setCertificationStatus(CertificationStatusEnum.PROGRESSING.getStatus());
        audit.setAuditStatus(0);
        save(audit);
    }

    /**
     * 机构认证审核分页查询
     * @param dto
     * @return
     */
    @Override
    public PageResult<AgencyCertificationAuditResDTO> pageQueryAgency(AgencyCertificationAuditPageQueryReqDTO dto) {
        String name = dto.getName();
        Integer certificationStatus = dto.getCertificationStatus();
        Integer auditStatus = dto.getAuditStatus();
        String legalPersonName = dto.getLegalPersonName();
        PageResult<AgencyCertificationAuditResDTO> result = PageHelperUtils.selectPage(dto,
                () -> agencyMapper.pageQueryAgency(dto));
        if (CollUtils.isEmpty(result.getList())) {
            return PageResult.empty(0L, 0L);
        }
        return result;
    }

    /**
     * 审核机构人员提交的认证
     * @param id
     * @param dto
     */
    @Override
    @Transactional
    public void auditAgency(Long id, CertificationAuditReqDTO dto) {
        AgencyCertificationAudit audit = getById(id);
        if (ObjectUtils.isNull(audit)) {
            throw new DBException("待审核的认证信息不存在");
        }
        String rejectReason = dto.getRejectReason();
        Integer certificationStatus = dto.getCertificationStatus();
        boolean success = lambdaUpdate()
                .eq(AgencyCertificationAudit::getId, id)
                .set(certificationStatus != null, AgencyCertificationAudit::getCertificationStatus, certificationStatus)
                .set(rejectReason != null, AgencyCertificationAudit::getRejectReason, rejectReason)
                // 设置审核状态为已审核
                .set(AgencyCertificationAudit::getAuditStatus, 1)
                .set(AgencyCertificationAudit::getAuditorId, UserContext.currentUserId()) // 设置审核人id
                .set(AgencyCertificationAudit::getAuditorName, UserContext.currentUser().getName()) // 设置审核人名称
                .set(AgencyCertificationAudit::getAuditTime, LocalDateTime.now())
                .update();
        if (!success) {
            throw new DBException("审核认证信息失败");
        }
        // 更新机构人员申请的审核状态
        AgencyCertification agencyCertification = agencyMapper.selectById(audit.getServeProviderId());
        agencyCertification.setCertificationStatus(certificationStatus);
        agencyCertification.setCertificationTime(LocalDateTime.now());
        agencyMapper.updateById(agencyCertification);
    }
}
